﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XOS_VHD_Tools.VHD;
using System.ComponentModel;

namespace XOS_VHD_FAT32
{
    abstract class FSItem : IFSEntry, IShortName, INotifyPropertyChanged
    {
        protected readonly DirEntry _entry;
        protected readonly FAT32Driver _drv;

        public DirEntry FAT32DirEntry { get { return _entry; } }

        public FSItem(DirEntry entry, FAT32Driver drv)
        {
            if (drv == null)
            {
                throw new ArgumentNullException("drv");
            }

            _entry = entry;
            _drv = drv;
        }

        protected string _name;
        public string Name
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_name))
                {
                    return ShortName;
                }
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public string ShortName
        {
            get
            {
                var ext = _entry.Name.Substring(8).Trim();
                return (ext.Length == 0 ? _entry.Name.TrimEnd() : _entry.Name.Substring(0, 8).TrimEnd() + "." + ext).ToUpper();
            }
        }

        public IDirectory ParentDirectory
        {
            get;
            internal set;
        }

        protected void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
